from selenium import webdriver

# Chrome浏览器
from selenium.webdriver.common.by import By

from DB import Db


class SeleniumGet:
    option = webdriver.ChromeOptions()
    option.add_argument('headless')
    driver = webdriver.Chrome(options=option)
    driver.get("https://voice.baidu.com/act/newpneumonia/newpneumonia/?from=osari_aladin_banner")

    add_local = driver.find_element(By.XPATH,
                                    '/html/body/div[2]/div/div/div/section/div[2]/div[1]/div[2]/div[1]/div[2]')  # 新增本土
    add_GOT = driver.find_element(By.XPATH, '//*[@id="ptab-0"]/div[1]/div[2]/div[4]/div[2]')  # 港澳台新增
    add_overseas = driver.find_element(By.XPATH, '//*[@id="ptab-0"]/div[1]/div[2]/div[3]/div[2]')  # 新增境外
    add_local_none = driver.find_element(By.XPATH, '//*[@id="ptab-0"]/div[1]/div[2]/div[2]/div[2]')  # 新增本土无症状

    localing = driver.find_element(By.XPATH, '//*[@id="ptab-0"]/div[1]/div[3]/div[1]/div[2]')  # 现有本土
    overseas_ing = driver.find_element(By.XPATH, '//*[@id="ptab-0"]/div[1]/div[3]/div[3]/div[2]')  # 现有境外
    localing_none = driver.find_element(By.XPATH, '//*[@id="ptab-0"]/div[1]/div[3]/div[2]/div[2]')  # 现有本土无症状
    critical = driver.find_element(By.XPATH, '//*[@id="ptab-0"]/div[1]/div[3]/div[4]/div[2]')  # 重症

    confirmed = driver.find_element(By.XPATH, '//*[@id="ptab-0"]/div[1]/div[4]/div[1]/div[2]')  # 累计确诊
    add_up_overseas = driver.find_element(By.XPATH, '//*[@id="ptab-0"]/div[1]/div[4]/div[2]/div[2]')  # 累计境外
    add_up_cured = driver.find_element(By.XPATH, '//*[@id="ptab-0"]/div[1]/div[4]/div[3]/div[2]')  # 累计治愈
    add_up_died = driver.find_element(By.XPATH, '//*[@id="ptab-0"]/div[1]/div[4]/div[4]/div[2]')  # 累计死亡

    confirmed_yesterday = driver.find_element(By.XPATH, '//*[@id="ptab-0"]/div[1]/div[4]/div[1]/div[3]/span')  # 确诊较昨日
    add_up_overseas_yesterday = driver.find_element(By.XPATH,
                                                    '//*[@id="ptab-0"]/div[1]/div[4]/div[2]/div[3]/span')  # 累计境外较昨日
    add_up_cured_yesterday = driver.find_element(By.XPATH,
                                                 '//*[@id="ptab-0"]/div[1]/div[4]/div[3]/div[3]/span')  # 累计治愈较昨日
    add_up_died_yesterday = driver.find_element(By.XPATH,
                                                '//*[@id="ptab-0"]/div[1]/div[4]/div[4]/div[3]/span')  # 累计死亡较昨日

    # print(add_local.text, add_GOT.text, add_local_none.text, add_overseas.text, )
    # print(localing.text, overseas_ing.text, localing_none.text, critical.text)
    # print(confirmed.text, add_up_overseas.text, add_up_cured.text, add_up_died.text)
    # print(confirmed_yesterday.text, add_up_overseas_yesterday.text, add_up_cured_yesterday.text,
    #       add_up_died_yesterday.text)

    db = Db(host='localhost', user='root', password='123456', charset='utf8', db='django_project')

    # 每次插入前先清空表
    del_sql = 'truncate visual_integrateddata'
    db.others(del_sql)
    insert_data_sql = "insert into visual_integrateddata values (null,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s);"
    db.insert_many(insert_data_sql,
                   [add_local.text, add_local_none.text, add_overseas.text, add_GOT.text,
                    localing.text, localing_none.text, overseas_ing.text, critical.text,
                    confirmed.text, add_up_overseas.text, add_up_cured.text, add_up_died.text,
                    confirmed_yesterday.text, add_up_overseas_yesterday.text, add_up_cured_yesterday.text,
                    add_up_died_yesterday.text])
    db.close()  # 数据库连接关闭
    driver.close()
